package com.yyn.edumsm.controller;

import com.yyn.R;
import com.yyn.edumsm.service.MsmService;
import com.yyn.edumsm.utils.RandomUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("/edumsm/msm")
public class MsmController {
    @Autowired
    private MsmService msmService;
    @Autowired
    private RedisTemplate redisTemplate;

    //    发送短信
    @GetMapping("send/{phone}")
    public R sendMsm(@PathVariable String phone) {
        String code = (String) redisTemplate.opsForValue().get(phone);
        if (StringUtils.isNotEmpty(code)) {
            return R.success();
        }
            RandomUtil randomUtil = new RandomUtil();
            code = randomUtil.getFourBitRandom();
            Map<Object, Object> param = new HashMap<>();
            param.put("code", code);
            boolean send = msmService.send(param, phone);
        if(send){
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        }
            return R.success();


        }
    }
